package com.cpr.demo.mycraft.executor;

import ch.qos.logback.classic.util.CopyOnInheritThreadLocal;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Chen Panrui
 * created at 2021/9/10 11:02
 *
 * Q: 为什么会有这个类？
 * A: 自己尝试解决这个问题 ↓
 * 我希望能做到 在线程池中work线程执行时获取的上下文为 执行 该work线程的那个线程的上下文 ，而不是 创建 worker线程的 那个线程的上下文。
 *
 * 大致实现思路：把执行线程的上下文 copy给 线程池worker线程，在finally里面 删除 worker线程的上下文
 */
public class MyThreadPoolExecutorExecutor extends ThreadPoolExecutor {

    @Override
    public void execute(Runnable command) {
        super.execute(new Runnable() {

            @Override
            public void run() {
                command.run();
            }
        });
    }

    public MyThreadPoolExecutorExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public MyThreadPoolExecutorExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public MyThreadPoolExecutorExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public MyThreadPoolExecutorExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }
}
